package com.itheima.reggie.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gakki
 * @create 2022/4/30 15:48
 */
@Data
@ApiModel("响应实体")
public class Result<R> implements Serializable {

    @ApiModelProperty("响应编码")
    private Integer code;

    @ApiModelProperty("响应信息")
    private String msg;

    @ApiModelProperty("响应数据")
    private R data;

    private Map map = new HashMap();

    @ApiOperation("数据")
    public static <R> Result<R> success(R object) {
        Result result = new Result();
        result.data = object;
        result.code = 1;
        return result;
    }

    @ApiOperation("错误信息")
    public static <R> Result<R> error(String msg) {
        Result result = new Result();
        result.msg = msg;
        result.code = 0;
        return result;
    }

    @ApiOperation("动态数据")
    public Result<R> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }
}
